package com.mmp.common.util;

import java.text.DecimalFormat;
import java.util.Arrays;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import com.mmp.common.logging.BasicLogger;
import com.mmp.common.logging.LOGGER_TYPE;
import com.mmp.common.logging.MyLogger;
import com.mmp.common.logging.NothingLogger;
import com.mmp.main.util.Default;

public class Common {

	private final static LOGGER_TYPE loggingCapabilities = Default.lOG_LEVEL;
	private final static BasicLogger logger;

	static {
		if (loggingCapabilities == LOGGER_TYPE.FULLY_FEATURED)
			logger = new MyLogger();
		else
			logger = new NothingLogger();
	}

	public static void log(Exception ex) {
		logger.log(ex);
	}

	public static void log(String str) {
		logger.log(str);
	}

	public static String concatenateValues(Object[] values) {
		String result = "";
		if (values != null) {
			for (Object v : values) {
				// if (!result.isEmpty())
				result += Default.MULTIVALUE_SEPARATOR;
				result += v.toString();
			}
			result += Default.MULTIVALUE_SEPARATOR;
		}
		return ("".equals(result)) ? null : result;
	}

	public static String[] getConcatenatedValues(String concatenatedValuesStr, boolean sort) {
		if (concatenatedValuesStr == null)
			return null;

		if (StringUtils.countMatches(concatenatedValuesStr, Default.MULTIVALUE_SEPARATOR) < 2)
			return new String[] { concatenatedValuesStr };

		String[] valuesAry = concatenatedValuesStr.split("\\" + Default.MULTIVALUE_SEPARATOR);

		// remove the first element -- always empty
		valuesAry = Arrays.copyOfRange(valuesAry, 1, valuesAry.length);

		// sort the array if requested
		if (sort)
			Arrays.sort(valuesAry);
		/*
		 * Arrays.sort(valuesAry, new Comparator<String>() { 
		 * public int compare(String s1, String s2) { 
		 * return Integer.valueOf(s1).compareTo(Integer.valueOf(s2)); 
		 * } 
		 * });
		 */
		return valuesAry;

	}

	/*
	 * public static String getDisplayString(String inputStr, String defaultStr,
	 * boolean forHTMLView) { if (inputStr != null &&
	 * !inputStr.trim().isEmpty()) { if (forHTMLView) return
	 * StringEscapeUtils.escapeHtml(inputStr); else return
	 * StringEscapeUtils.escapeJava(inputStr); } return defaultStr; }
	 * 
	 * public static String getDisplayString(String inputStr, String
	 * defaultStr){ return getDisplayString( inputStr, defaultStr, false); }
	 */
	public static String getDisplayString(Object inputStr, String defaultStr, boolean embeddedDirectlyInHtml) {
		if (inputStr != null && !inputStr.toString().trim().isEmpty()) {
			if (embeddedDirectlyInHtml)
				return StringEscapeUtils.escapeHtml(inputStr.toString());
			else
				return StringEscapeUtils.escapeJava(inputStr.toString());
		}
		return defaultStr;
	}

	public static String getDisplayString(Object inputStr, String defaultStr) {
		return getDisplayString(inputStr, defaultStr, false);
	}

	public static int getDisplayBooleanAsInt(Boolean inputBool) {
		return inputBool.compareTo(false);
	}

	public static String formatMoneyValue(float value) {
		return new DecimalFormat("0.00").format(value);
	}

}
